﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using QuanLyThuVien.DTO;
using System.Data;
using QuanLyThuVien.Supporter;
using System.Data.SqlClient;

namespace QuanLyThuVien.DAL
{
    //----------------------------------------------------------------------
    //Desc: class kết nối với bảng CTTK_MuonSach
    //----------------------------------------------------------------------
    public class DALCTTK_MuonSach
    {
        //----------------------------------------------------------------------
        //Desc: constructor mặc định
        //----------------------------------------------------------------------
        public DALCTTK_MuonSach()
        { }

        //----------------------------------------------------------------------
        //Desc: chuyển dòng dữ liệu thành CTTK_MuonSach
        //----------------------------------------------------------------------
        public DTOCTTK_MuonSach ConvertToCTTKMuonSach(DataRow row)
        {
            DTOCTTK_MuonSach cttkMuonSach = new DTOCTTK_MuonSach();
            try
            {
                cttkMuonSach.ThangTK = (int)row["ThangTK"];
            }
            catch { }
            try
            {
                cttkMuonSach.NamTK = (int)row["NamTK"];
            }
            catch { }
            cttkMuonSach.TheLoai.MaTheLoai = row["MaTL"].ToString();
            try
            {
                cttkMuonSach.SoLuotMuon = long.Parse(row["SoLuotMuon"].ToString());
            }
            catch { }
            try
            {
                cttkMuonSach.TiLe = float.Parse(row["TiLe"].ToString());
            }
            catch { }


            return cttkMuonSach;
        }

        //----------------------------------------------------------------------
        //Desc: Lấy danh sách CTTK_MuonSach
        //----------------------------------------------------------------------
        public List<DTOCTTK_MuonSach> LayCTTKMuonSach(int thang, long nam)
        {
            if (DatabaseManager.DbConnection == null)
                return null;
            string spName = "spCTTK_MuonSach_LayDanhSachCTTK";
            SqlParameter sqlprThangTK = new SqlParameter("@ThangTK", SqlDbType.Int) { Value = thang };
            SqlParameter sqlprNamTK = new SqlParameter("@NamTK", SqlDbType.Int) { Value = nam };
            DataTable data = DatabaseManager.DbConnection.ExecuteStoredProcedure(spName, sqlprThangTK, sqlprNamTK);
            if (data == null)
                return null;
            List<DTOCTTK_MuonSach> list = new List<DTOCTTK_MuonSach>();
            foreach (DataRow row in data.Rows)
                list.Add(ConvertToCTTKMuonSach(row));

            return list;
        }

        //----------------------------------------------------------------------
        //Desc: Cập nhật CTTK_MuonSach
        //----------------------------------------------------------------------
        public bool CapNhat(DTOCTTK_MuonSach cttkMuonSach)
        {
            if (DatabaseManager.DbConnection == null)
                return false;
            string spName = "spCTTK_MuonSach_CapNhatCTTK";
            SqlParameter sqlprThaoTac = new SqlParameter("@ThaoTac", SqlDbType.Int) { Value = 1 };
            SqlParameter sqlprThangTK = new SqlParameter("@ThangTK", SqlDbType.Int) { Value = cttkMuonSach.ThangTK };
            SqlParameter sqlprNamTK = new SqlParameter("@NamTK", SqlDbType.Int) { Value = cttkMuonSach.NamTK };
            SqlParameter sqlprMaTL = new SqlParameter("@MaTL", SqlDbType.VarChar, 15) { Value = cttkMuonSach.TheLoai.MaTheLoai };
            SqlParameter sqlprSoLuotMuon = new SqlParameter("@SoLuotMuon", SqlDbType.Int) { Value = cttkMuonSach.SoLuotMuon };
            SqlParameter sqlprTiLe = new SqlParameter("@TiLe", SqlDbType.Float) { Value = cttkMuonSach.TiLe };
            return DatabaseManager.DbConnection.ExecuteStoredProcedureNonQuery(spName, sqlprThaoTac, sqlprThangTK, sqlprNamTK, sqlprMaTL, sqlprSoLuotMuon, sqlprTiLe);
        }
    }
}
